package com.math;

import java.util.Arrays;

/**
 * @ClassName BigInt
 * @Description TODO 大的int类型实现
 * @Author 王晨缘
 * @Date 2018-12-19 15:54
 * @Version 1.0
 **/
public class BigInt {
    private String str;

    public BigInt() {

    }

    public BigInt(String str) {
        this.str = str;
    }

    public int[] getIntArray() {
        char[] chars = str.toCharArray();
        int[] ints = new int[chars.length];
        for (int i = 0; i < chars.length; i++) {
            ints[i] = Integer.parseInt(String.valueOf(chars[i]));
        }
        return ints;
    }

    @Override
    public String toString() {
        return str;
    }

    public BigInt add(BigInt b) {
        int[] aIntArray = this.getIntArray();
        int[] bIntArray = b.getIntArray();
        int[] ints = null;
        BigInt jisuan = null;
        if (aIntArray.length > bIntArray.length) {
            ints = Arrays.copyOf(aIntArray, aIntArray.length + 1);
            jisuan = jisuan(aIntArray, bIntArray, ints);
        } else {
            ints = Arrays.copyOf(bIntArray, bIntArray.length + 1);
            jisuan = jisuan(bIntArray, aIntArray, ints);
        }
        return jisuan;
    }

    private BigInt jisuan(int[] aIntArray, int[] bIntArray, int[] ints) {
        int j = bIntArray.length - 1;
        int p = 0;
        s:
        for (int i = aIntArray.length - 1; i >= 0; i--) {
            int i1 = 0;
            if (j < 0) {
                i1 = aIntArray[i] + p;
            } else {
                i1 = aIntArray[i] + bIntArray[j] + p;
            }
            if (i1 >= 10) {
                ints[ints.length - 2 - i] = i1 % 10;
                p = 1;
            } else {
                ints[ints.length - 2 - i] = i1;
                p = 0;
            }
            j--;
        }
        String s = "";
        for (int i = ints.length - 1; i >= 0; i--) {
            if ("".equals(s) && ints[i] == 0) {
                continue;
            }
            s += ints[i];
        }
        BigInt bigInt = new BigInt(s);
        return bigInt;
    }

    public static void main(String[] args) {
        BigInt a = new BigInt("22289");
        BigInt b = new BigInt("1099");
        BigInt add = b.add(a);
        System.out.println(add.toString());
    }
}
